/*
 * ToolBarDelegate.cpp  0.4.0 / Dec 05, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "ToolBarDelegate.h"

// ToolBarDelegate :: Protected Constructor. -----------------------------------

/*!
 * \brief Constructs new ToolBarDelegate object.
 *
 * \param env the JNIEnv pointer
 * \param obj the ToolBar java object
 */
ToolBarDelegate::ToolBarDelegate(JNIEnv *env, jobject obj)
  : ContainerDelegate(env, obj, new QToolBar())
{
  connectDirect(qToolBar(), SIGNAL(orientationChanged(Qt::Orientation)),
                SLOT(orientationChangedSlot(Qt::Orientation)));
}

// ToolBarDelegate :: Native Calls. --------------------------------------------

/*!
 * \brief Sets object name.
 *
 * \param env the JNIEnv pointer
 * \param name the tool bar title name
 */
void ToolBarDelegate::setName(JNIEnv *env, jstring name)
{
  Q_ASSERT(env);
  Q_ASSERT(name);
  SWING_REENTRANT(setName, SWING_ENV_ARG, JSTRING_ARG(name))
  QStringAdapter title(env, name);
  qToolBar()->setObjectName(title);
  qToolBar()->setWindowTitle(title);
}

/*!
 * \brief Inserts child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child pointer
 * \param index the child index
 */
void ToolBarDelegate::insertChild(JNIEnv *env, jlong childDelegate, jint index)
{
  Q_ASSERT(env);
  SWING_REENTRANT(insertChild, SWING_ENV_ARG, JLONG_ARG(childDelegate),
                  JINT_ARG(index))
  QAction *before = 0;
  if ((index >= 0) && (index < qToolBar()->actions().size())) {
    before = qToolBar()->actions().at(index);
  }
  ComponentDelegate *childComponentDelegate = COMPONENT_CAST(childDelegate);
  qToolBar()->insertAction(before, childComponentDelegate->qAction());
  childComponentDelegate->setParentDelegate(this);
  childComponentDelegate->setParent(this);
}

/*!
 * \brief Removes child.
 *
 * \param env the JNIEnv pointer
 * \param childDelegate the child delegate
 */
void ToolBarDelegate::removeChild(JNIEnv *env, jlong childDelegate)
{
  Q_ASSERT(env);
  SWING_REENTRANT(removeChild, SWING_ENV_ARG, JLONG_ARG(childDelegate))
  ComponentDelegate *childComponentDelegate = COMPONENT_CAST(childDelegate);
  qToolBar()->removeAction(childComponentDelegate->qAction());
  childComponentDelegate->setParentDelegate(0);
  childComponentDelegate->setParent(0);
}

/*!
 * \brief Sets whether or not toolbar can be moved.
 *
 * \param env the JNIEnv pointer
 * \param floatable \c true if the toolbar can be moved
 */
void ToolBarDelegate::setFloatable(JNIEnv *env, jboolean floatable)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setFloatable, SWING_ENV_ARG, JBOOLEAN_ARG(floatable))
  qToolBar()->setMovable(floatable);
}

/*!
 * \brief Sets toolbar orientation
 *
 * \param env the JNIEnv pointer
 * \param orientation the orientation
 */
void ToolBarDelegate::setOrientation(JNIEnv *env, jint orientation)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setOrientation, SWING_ENV_ARG, JINT_ARG(orientation))
  switch(orientation) { // do not cast
    case 0:
      qToolBar()->setOrientation(Qt::Horizontal);
      break;
    case 1:
      qToolBar()->setOrientation(Qt::Vertical);
      break;
  }
}

/*!
 * \brief Sets icon size.
 *
 * \param env the JNIEnv pointer
 * \param width the width
 * \param height the height
 */
void ToolBarDelegate::setIconSize(JNIEnv *env, jint width, jint height)
{
  Q_ASSERT(env);
  SWING_REENTRANT(setOrientation, SWING_ENV_ARG, JINT_ARG(width),
                  JINT_ARG(height))
  qToolBar()->setIconSize(QSize(width, height));
}

// ToolBarDelegate :: Static Native Calls. -------------------------------------

/*!
 * \brief Initializes ToolBarDelegate class.
 *
 * \param env the JNIEnv pointer
 * \param cls the ToolBar java class
 *
 * \exception FatalError if failed
 */
void ToolBarDelegate::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_orientationChangedMID);
  if (!INIT_UMID(orientationChanged, cls, "(I)V")) {
    FATAL_LOGGED_ERROR("ToolBarDelegate initialization failed");
  }
}

/*!
 * \brief Returns new instance of the delegate object
 *
 * \param env the JNIEnv pointer
 * \param obj the Swing object
 * 
 * \return new instance of the delegate object
 */
jlong ToolBarDelegate::newInstance(JNIEnv *env, jobject obj)
{
  if (JNISwingWrapper::isSwingThread(env)) {
    return reinterpret_cast<jlong>(new ToolBarDelegate(env, obj));
  }
  return SwingUtilitiesDelegate::invokeLongAndWait(env, obj, createMID());
}

// ToolBarDelegate :: Native Callback. -----------------------------------------

/*!
 * \brief Calls \c orientationChanged method of the toolbar object.
 *
 * \param orientation the orientation
 */
void ToolBarDelegate::orientationChanged(jint orientation)
{
  jniObject()->callVoidMethod(sm_orientationChangedMID, orientation);
}

// ToolBarDelegate :: Protected Method. ----------------------------------------

/*!
 * \brief orientationChanged slot.
 *
 * \param orientation the orientation
 */
void ToolBarDelegate::orientationChangedSlot(Qt::Orientation orientation)
{
  switch(orientation) {
    case Qt::Horizontal:
      orientationChanged(0);
      break;
    case Qt::Vertical:
      orientationChanged(1);
      break;
  }
}

// ToolBarDelegate :: Private Static Data Member. ------------------------------

jmethodID ToolBarDelegate::sm_orientationChangedMID = 0;
